package DepartmentManagement

import (
	"MedicalTreatment/global"
	model "MedicalTreatment/model"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func AddDep(r *gin.Engine) {
	//新增科室接口
	r.POST("addDel", func(context *gin.Context) {
		var dep model.Department
		if err := context.ShouldBindJSON(&dep); err != nil {
			context.JSON(200, gin.H{
				"Msg": err,
			})
		} else {
			if err := global.DB.Where("name = ?", dep.Name).First(&dep).Error; err != nil {
				if err == gorm.ErrRecordNotFound {
					if err := global.DB.Create(&dep).Error; err != nil {
						context.JSON(200, gin.H{
							"Msg": err,
						})
					} else {
						context.JSON(200, gin.H{
							"Msg": "创建成功",
						})
					}
				} else {
					context.JSON(200, gin.H{
						"Msg": err,
					})
				}
			}
		}
	})
}
